const app = getApp()
var address = []//地区数据
Page({
  /**
   * 页面的初始数据
   */
  /**
    * 页面的初始数据
    */
  data: {
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0,0,0],
    provinces: [],
    citys: [],
    areas: [],
    province: '',
    city: '',
    area: '',
    display: "none",
    currentID: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this;
    // 初始化动画变量
    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    that.animation = animation;

    // 默认联动显示北京
    address = wx.getStorage({
      key: 'AllArea',
      success: function (res) {
        if (address == null) {
          that.getAllArea();
        }
        else {
          that.setAllArea();
        }
      },
      fail: function (res) {
        that.getAllArea();
      },
      complete: function (res) { },
    })

  },

  //获取地址库
  getAllArea: function () {
    var that = this;
    wx.request({
      url: app.globalData.api + 'getProvinces',
     
      success: function (res) {
        console.log('省',res)
        var result = res.data.list;
        that.setData({
          provinces: result
        })
       
      },
    
    });
    wx.request({
      url: app.globalData.api + 'getCities',
      data:{
        provinceid: 110000
      },
      success: function (res) {
        console.log('市',res)
        var result = res.data.list;
        that.setData({
          citys: result
        })

      },
    
    });
    wx.request({
      url: app.globalData.api + 'getAreas',
      data:{
        cityid: 110200
      },
      success: function (res) {
        console.log('区',res)
        var result = res.data.list;
        that.setData({
          areas: result
        })
  
      },
      
    });
  },

  //设置默认地址信息
  setAllArea: function () {
    var id = address.provinces[0].id
    this.setData({
      provinces: address.provinces,
      citys: address.citys[id],
      areas: address.areas[address.citys[id][0].id],
    })
  },

  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    var that = this
    // 如果已经显示，不在执行显示动画
    if (that.data.addressMenuIsShow) {
      return
    }
    // 执行显示动画
    that.startAddressAnimation(true)
  },

  // 执行动画
  startAddressAnimation: function (isShow) {
    var that = this
    if (isShow) {
      // vh是用来表示尺寸的单位，高度全屏是100vh
      that.animation.translateY(0 + 'vh').step();
      //显示遮罩层
      that.setData({
        display: "block"
      })

    } else {
      that.animation.translateY(40 + 'vh').step();
      //隐藏遮罩层
      that.setData({
        display: "none"
      })
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    var city = that.data.city
    var value = that.data.value
    console.log()
    that.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    var areaInfo = that.data.provinces[value[0]].province + ',' + that.data.citys[value[1]].city + ',' + that.data.areas[value[2]].area;
    var id = that.data.areas[value[2]].id;
    that.setData({
      areaInfo: areaInfo,
      currentID: id,
    })
  },
  // 点击蒙版时取消组件的显示
  hideCitySelected: function (e) {
    this.startAddressAnimation(true)
  },
  // 处理省市县联动逻辑
  cityChange: function (e) {
    let that = this
    console.log(e)
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    // 如果省份选择项和之前不一样，表示滑动了省份，此时市默认是省的第一组数据，
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      wx.request({
        url: app.globalData.api + 'getCities',
        data: {
          provinceid: provinces[provinceNum].provinceid
        },
        success: function (res) {
          console.log('市', res)
          let result = res.data.list;
          that.setData({
            citys: result
          })
          let cityid = res.data.list[0].cityid
          wx.request({
            url: app.globalData.api + 'getAreas',
            data: {
              cityid: cityid
            },
            success: function (res) {
              console.log('区', res)
              let result = res.data.list;
              that.setData({
                areas: result,
                value: [provinceNum, 0, 0]
              })

            },

          });
         
        },

      });
   
    } else if (this.data.value[1] != cityNum) {
      // 滑动选择了第二项数据，即市，此时区显示省市对应的第一组数据
      var id = citys[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        // areas: address.areas[citys[cityNum].id],
      })
    } else {
      // 滑动选择了区
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.load()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})